<template>
  <el-card style="width: 1200px; margin: 20px auto" shadow="always">
    <el-form inline :disabled="isdisable !== '1'">
      <el-form-item label="一级分类">
        <el-select
          v-model="Category1Id"
          style="width: 250px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in Category1List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="150px" label="二级分类">
        <el-select
          v-model="Category2Id"
          no-data-text="暂无数据"
          placeholder="请选择"
          style="width: 250px"
        >
          <el-option
            v-for="item in Category2List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="150px" label="三级分类">
        <el-select
          style="width: 250px"
          v-model="Id"
          no-data-text="暂无数据"
          placeholder="请选择"
        >
          <el-option
            v-for="item in Category3List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import {
  getCategory1Api,
  getCategory2Api,
  getCategory3Api
} from '@/api/SortApi'
export default {
  name: 'Sort',
  data() {
    return {
      Category1Id: '',
      Category2Id: '',
      Id: '',
      Category1List: [],
      Category2List: [],
      Category3List: []
    }
  },
  computed: {
    Category3Id() {
      return this.$store.state.sortStore.Category3Id
    }
  },
  watch: {
    Category1Id() {
      this.getCategory2(this.Category1Id)
      this.Category2Id = ''
      this.Id = ''
    },
    Category2Id() {
      if (!this.Category2Id) return
      this.getCategory3(this.Category2Id)
      this.Id = ''
    },
    Id: {
      handler(newValue) {
        this.$store.dispatch('sortStore/ChangeId', {
          Category1Id: this.Category1Id,
          Category2Id: this.Category2Id,
          Category3Id: newValue
        })
      },
      immediate: true
    }
  },
  mounted() {
    this.getCategory1()
  },
  methods: {
    async getCategory1() {
      const res = await getCategory1Api()
      if (res.code === 200) {
        this.Category1List = res.data
      }
    },
    async getCategory2(id) {
      const res = await getCategory2Api(id)
      if (res.code === 200) {
        this.Category2List = res.data
      }
    },
    async getCategory3(id) {
      const res = await getCategory3Api(id)
      if (res.code === 200) {
        this.Category3List = res.data
      }
    }
  },
  props: {
    isdisable: {
      type: String
    }
  }
}
</script>

<style lang="less" scoped></style>
